home *** CD-ROM | disk | FTP | other *** search
- Path: eerie.acsu.buffalo.edu!newserve!rebecca!rpi!not-for-mail
- From: rmartin@oma.com (Robert C. Martin)
- Newsgroups: comp.lang.c++.moderated,comp.lang.c++
- Subject: Re: Meaning of the specifier volatile?
- Date: 4 Jan 1996 02:49:08 -0000
- Organization: Object Mentor
- Sender: cppmods@netlab.cs.rpi.edu
- Approved: herbs@connobj.com
- Message-ID: <4cff74$gfj@netlab.cs.rpi.edu>
- References: <4c9740$27n@netlab.cs.rpi.edu>
- NNTP-Posting-Host: netlab.cs.rpi.edu
-
- X-Original-Date: 03 Jan 1996 19:38:59 GMT
-
- In article <4c9740$27n@netlab.cs.rpi.edu> Srinivas Vobilisetti
- <srv@cs.wayne.edu> writes:
-
- Nowhere i could find exact meaning of the specifier volatile.
-
- Consider the following piece of code:
-
- int n;
- for (int i=0; i<99; i++)
- n=0;
-
- A good optimising compiler should rewrite this as:
-
- int n=0;
- int i=100;
-
- {That's right, if you want to delay for 100 micro seconds, the above
- is not a portable approach.}
-
- The use of the keyword volatile prevents the compiler from optimising
- the reads and writes to a variable.
-
- volatile int n;
- for (int i=0; i<99; i++)
- n=0;
-
- Will cause zero to be written into the variable n 100 times.
- {Note, the compiler may be smart enough to do this without using the
- variable i!}.
-
- volatile int n;
- for (volatile int i=0; i<99; i++)
- n=0;
-
- Will cause zero to be written into the variable n 100 times. It will
- also cause the variable i to be incremented inbetween writes to n, and
- will read i to test it for 99 each time through the loop.
-
- Generally, this is used when certain memory registers have side
- effects. e.g.
-
- int * volatile tty = 0x100; // 0x100 is the addr of the tty device.
-
- *tty = 'h'; // prints a 'c' on the tty;
-
- void PrintMessage(char *s)
- {
- for(; *s; s++) *tty = *s; // prints the string on the tty.
- }
-
- You can also use volatile if you want to create stupid delays as in
- the first example.
-
- --
- Robert Martin | Design Consulting | Training courses offered:
- Object Mentor Assoc.| rmartin@oma.com | OOA/D, C++, Advanced OO
- 14619 N. Somerset Cr| Tel: (708) 918-1004 | Mgt. Overview of OOT
- Green Oaks IL 60048 | Fax: (708) 918-1023 | Development Contracts.
-
-
- [ comp.lang.c++.moderated is a moderated newsgroup. Submit articles ]
- [ to <c++-submit@netlab.cs.rpi.edu>. The moderation policy can be ]
- [ retrieved from <http://netlab.cs.rpi.edu/~cppmods/guide.html>. ]
- [ Moderators can be reached at: c++-request@netlab.cs.rpi.edu. ]
-